/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DataAccess;

import Business.BusinessDelegate;
import Business.File.Percorsi;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Domenico
 */
public class ClienteFornitoreHtml {
    private static final String STARTPH = "<!-- Value = ";
    private static final String ENDPH = " /Value -->";
    private static final String NOMEFILE = "ClienteFornitore.html";
    ResultSet resultset = null;
    String html = null;
    private ArrayList<String> listaFileCorrelati = new ArrayList<String>();
    public ClienteFornitoreHtml(ResultSet rs) {
        
            resultset = rs;
            html = BusinessDelegate.ottieniTestoFile(Percorsi.PERCORSO_FILE_HTML + NOMEFILE);
            listaFileCorrelati.add(Percorsi.PERCORSO_FILE_HTML + "ClienteFornitore.css");
            
    }
    public boolean inserisciValori() {
        String testo = parseText(html);
        BusinessDelegate.creaDocumentoHTML(Percorsi.PERCORSO_FILE_TEMPORANEI, NOMEFILE, testo, listaFileCorrelati);
        return true;
    }
    private String parseText(String text) {
        String tempText = text;
        int index = -1;
        index = tempText.indexOf(STARTPH);
        int indexEnd = tempText.indexOf(ENDPH);
        while ((index > -1) && (indexEnd > -1)) {
            int startPlaceHolder = index;
            int endPlaceHolder = indexEnd;
            String part1 = tempText.substring(0, startPlaceHolder);
            String part2 = tempText.substring(endPlaceHolder + ENDPH.length());
            String placeHolder = tempText.substring(startPlaceHolder, endPlaceHolder);
            
            //Removing comment
            placeHolder = placeHolder.substring(STARTPH.length());
            //placeHolder = placeHolder.substring(0, placeHolder.length() - ENDPH.length());
            String value = null;
            try {
                
                value = findValue(placeHolder);
            } catch (SQLException ex) {
                System.out.println(placeHolder);
                Logger.getLogger(ClienteFornitoreHtml.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ParseException ex) {
                System.out.println(placeHolder);
                Logger.getLogger(ClienteFornitoreHtml.class.getName()).log(Level.SEVERE, null, ex);
            }
            tempText = part1 + value + part2;
            index = tempText.indexOf(STARTPH);
            indexEnd = tempText.indexOf(ENDPH);
        }
        return tempText;
    }
        
        
    
    private String findValue(String input) throws SQLException, ParseException {
        String prova = null;
        resultset.first();
        Object a;
        if (resultset.getMetaData().getColumnType(findColumnIndex(input)) == java.sql.Types.DATE){
            SimpleDateFormat s = new SimpleDateFormat("dd/MM/yyyy");
            Date data = resultset.getDate(input);
            
            
            a = s.format(data);
            
        } else {
            a = resultset.getObject(input);
        }
         
        prova = a.toString();
        //String prova = resultset.getRef(input).getObject().toString();
        return prova;
    }
    private int findColumnIndex(String columnName) throws SQLException {
        
        for (int i = 1; i <= resultset.getMetaData().getColumnCount(); i++) {
            if (resultset.getMetaData().getColumnLabel(i).equals(columnName)) {
                return i;
            }
        }
        return -1;
    }

}
